


from sketchz import *

window = WindowAfter((1000, 800))
#window = WindowCai((1000, 800))

from mat_encode import de
from itertools import product
from sdl2.ext import FontTTF
Rect = sdl2.SDL_Rect

font = FontTTF("tuffy.ttf", "25px", (0, 0, 0, 255))       # 尼玛这里就是255级的

all_text = sdl2.ext.surface._create_surface((10*120, 10 * 100), (255, 255, 255, 255)).contents
ads = Ads100(as_image(window.ctx, all_text))
p1 = [-1, 1, 0, 0, 0, 1,  0, 0]
p2 = [-1, -1, 0, 0, 0, 1, 0, 1]
p3 = [1, -1, 0, 0, 0, 1, 1, 1]
p4 = [1, 1, 0, 0, 0, 1,  1, 0]
arr = np.array([p1, p2, p3, p1, p3, p4], dtype="f4")
ads.vertices = arr[:, :3]
ads.normals = arr[:, 3:6]
ads.texcoords = arr[:, 6:]
ads.u(window)
ads >> window

box = Obj()
box.color = V3(1, 1, 1)
box >> window
box1 = make_block(U3(1/2, 1/2, 1/2))
quad = make_quad('xy', 1, 1)



data = []
now = 0

with open("23sorted.txt") as f:
# ~ with open("333sorted.txt") as f:
    for line in f:
        code = line.strip()
        mat = de(code)
        data.append((code, mat))

def calc():
    box.vertices.clear()
    box.normals.clear()

    pos_l = []
    piece_l = []
    sdl2.ext.fill(all_text, (255, 255, 255, 255))

    for (j, i), (code, mat) in zip(product(range(10), range(10)), data[now: now+100]):
        pos_l.append(U3(i * 7+0.5, j * 7+0.5, -3))
        piece_l.append((i * 1/10, j * 1/10, 1/10, 1/10))

        # ~ text = font.render_text(code[3:], width=150)
        # ~ text = font.render_text(code, width=150)
        text = font.render_text(code)
        sdl2.SDL_BlitSurface(text, None, all_text, Rect(i *120, j * 100, 100, 100))

        bbase = U3(i * 7, j * 7, 0)
        for bz, by, bx in product(range(2), range(2), range(2)):
            base = bbase + U3(bx * 0.5*len(mat[0][0]), by * 0.5*len(mat[0]), bz*0.5*len(mat))
            for z, y, x in product(range(len(mat)), range(len(mat[0])), range(len(mat[0][0]))):
                if mat[z][y][x] == 1:
                  # ~ box1.t.pos = base + U3(x, 2-y, z) / 2
                  box1.t.pos = base + U3(x, y, z) / 2
                  box1.as_unit() >> box

        for x, y in product(range(3), range(3)):
            quad.t.pos = bbase + U3(x, y, -0.5)
            quad.as_unit() >> box

    box.u(window)

    # ~ print(pos_l)
    ads.update100(pos_l, piece_l)
    ads.texture.write(as_image(window.ctx, all_text).read())


calc()



camera = window.camera
camera.t.pos = U3(0, -5, 1)

walker = Walker3(camera)
walker >> window

class C(Control):
    def down(self, window, e):
        global now
        if e.keysym.sym == sdl2.SDLK_SPACE:
            now = now + 100
            if now > len(data):
                now = 0
            print(now)
            calc()

    def reg_event(self, window):
        window.key_down_callback.append(self.down)


C() >> window

window.mainloop()
